package com.aos.eap.activity;

import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.RepairBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

/**
 * 更换螺丝
 */
public class EE_ChangeScrewActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvScrewOld)
    TextView tvScrewOld;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private RepairBean repairBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ee_change_screw;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        repairBean = (RepairBean) getIntent().getSerializableExtra("repairBean");
        tvEqpId.setText(repairBean.getEqpId());
        tvLotId.setText(repairBean.getLotId());
        tvScrewOld.setText(repairBean.getScrewCnt());
    }

    @Override
    protected void initData() {
        //寿命危险值25
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("螺丝更换");
        freeUI_titleView.setLeftListener(view -> showBackDialog(EE_ChangeScrewActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", repairBean.getEqpId());
            map.addProperty("LotID", repairBean.getLotId());
            map.addProperty("ScrewCnt", Float.parseFloat(repairBean.getScrewCnt()));
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_RepaireScrew", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(() -> {
                        setResult(2103);
                        EE_ChangeScrewActivity.this.finish();
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RepaireScrew");
            }
        });
    }

}
